//
//  CYModifyPasswordVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/5.
//  Copyright (c) 2015年 Lee. All rights reserved.
//  修改密码

#import "CYModifyPasswordVC.h"
#import "CYMeHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYEscape.h"
#import "CYJumpsTool.h"
#import "MBProgressHUD+MJ.h"
#import "GCD.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"
#import "CYHttpTool.h"

@interface CYModifyPasswordVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *newlyPasswordLabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneCodeLabel;
@property (weak, nonatomic) IBOutlet UILabel *sendSMSLabel;


@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *passsureTF;
@property (weak, nonatomic) IBOutlet UITextField *yzmTF;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIButton *yzmBtn;





@property(nonatomic, strong) GCDTimer *GCDTimer;
@end

@implementation CYModifyPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.submitBtn.layer.cornerRadius = 4.f;
    self.submitBtn.layer.masksToBounds = YES;
}


/**
 *  倒计时
 */
- (void)countdown:(UIButton *)btn {
    self.GCDTimer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    
    CYWeakObject(weakself, self);
    __block int time = 59;
    [self.GCDTimer event:^{
        if (time < 1) {
            //设置界面的按钮显示 根据自己需求设置
            weakself.sendSMSLabel.text = @"发送验证码";
            btn.userInteractionEnabled = YES;
            [weakself.GCDTimer destroy];
        } else {
             [_yzmBtn setTitle:[NSString stringWithFormat:@"%dS",time] forState:UIControlStateNormal];
            btn.userInteractionEnabled = NO;
        }
        time--;
    } timeInterval:NSEC_PER_SEC * 1];
    [self.GCDTimer start];
}

- (IBAction)submitClick:(id)sender {
     if (_passwordTF.text.length == 0) {
          CYAlertView(@"请输入新密码");
          return;
     }
     if (_passsureTF.text.length == 0) {
          CYAlertView(@"请确认新密码");
          return;
     }
     if (_yzmTF.text.length == 0) {
          CYAlertView(@"请输入短信验证码");
          return;
     }
     if (![_passwordTF.text isEqualToString:_passsureTF.text]) {
          CYAlertView(@"密码不一致！请核实");
          return;
     }
    
          CYUser *user = [CYLoginHttpTool userData].user;
          NSString *pw = [CYEscape encrypt:self.passwordTF.text];
          NSDictionary *params = @{@"newPassword": pw, @"userId": user.userId, @"token": user.token, @"phoneCode": self.yzmTF.text};
          [MBProgressHUD showMessage:nil toView:self.view];
     
          [CYHttpTool postWithURL:[CYURL modifyPasswordPath] params:params success:^(id json) {
               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
               CYLog(@"jsoninfo = %@",jsoninfo);
               CYAlertView(jsoninfo.message);
               [MBProgressHUD hideHUDForView:self.view animated:NO];
               if (jsoninfo.isSuccess) {
                    
                    [CYLoginHttpTool isClearUserData];
                    [self.GCDTimer destroy];
                    [CYJumpsTool jumpToMain:self.view];
                    
               }
               
               
          } failure:^(NSError *error) {
               [MBProgressHUD hideHUDForView:self.view animated:NO];
          }];
          
     
    [self.view.window endEditing:YES];
     
}
- (IBAction)yzmClick:(id)sender {
     
     [self countdown:sender];
     //检查网络
     //检查电话号码是否正确
     NSString *regex = @"^(1[0-9][0-9])\\d{8}$";
     CYUser *user = [CYLoginHttpTool userData].user;
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
     BOOL isPhone = [predicate evaluateWithObject:user.phone];
     if (!isPhone) {
          [MBProgressHUD showError:@"请输入正确的电话号码"];
          return;
     }
     
     //确认网络有连接后发送短信验证码
     [CYLoginHttpTool sendSMSWithPhone:user.phone success:^(id json) {
          //        CYLog(@"sms: %@", json);
          CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsonInfo.isSuccess) {
               [MBProgressHUD showSuccess:jsonInfo.message];
               
          }else {
               [MBProgressHUD showError:jsonInfo.message];
          }
     } failure:^(NSError *error) {
          [MBProgressHUD showError:CYServerConnectFailure];
          CYLog(@"服务器链接失败：%@", error);
     }];

}

#pragma mark - 键盘监听
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view.window endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
